package com.quan.OGCamera;

import java.io.File;

import com.example.test1.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

//This activity call the Camera Activity and handle its result
public class CameraControlActivity extends Activity {
	private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

	public static Uri fileUri;

	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.camera_control);

	    // create Intent to take a picture and return control to the calling application
	    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

	    fileUri = ImageManager.getTempMediaFileUri(ImageManager.MEDIA_TYPE_IMAGE,this); // create a file to save the image
	    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name 

	    // start the image capture Intent
	    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
	    

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		//Handling result from ACTION_IMAGE_CAPTURE
	    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
	        if (resultCode == RESULT_OK) {
	        	//Cache the new image
	            ImageManager.workingImage = BitmapFactory.decodeFile(fileUri.getPath());
	            File file = new File(fileUri.toString());
	            
	            //Some phone save another version of the captured image to Gallery
	            //Need to check if another version of the picture is kept by checking the lastModified of the latest picture in gallery
	            File file2 = ImageManager.lastFromDCIM();
	            if (Math.abs(file.lastModified()-file2.lastModified())<1000) {
	            	Log.d("banzai",String.format("%b", file.delete()));
	            	Log.d("banzai",String.format("%b", file2.delete()));
	            } else {
	            	file.delete();
	            }
	            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
	            //Start next activity
	        	Intent intent = new Intent(this, PickOverlayActivity.class);
	    		startActivity(intent);
	        } else if (resultCode == RESULT_CANCELED) {
	            // User cancelled the image capture
	        } else {
	            // Image capture failed, advise user
	        }
	    }
	    /*
	    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
	        if (resultCode == RESULT_OK) {
	            // Video captured and saved to fileUri specified in the Intent
	            Toast.makeText(this, "Video saved to:\n" +
	                     data.getData(), Toast.LENGTH_LONG).show();
	        } else if (resultCode == RESULT_CANCELED) {
	            // User cancelled the video capture
	        } else {
	            // Video capture failed, advise user
	        }
	    }*/
	}
	
	
	
}
